package com.gzdh.dishrecognition.thead;

import android.app.Activity;

import com.alibaba.fastjson.JSONObject;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.greendao.dao.OrdersDetailsEntityDao;
import com.greendao.dao.OrdersEntityDao;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.entity.OrdersDetailsEntity;
import com.gzdh.dishrecognition.entity.OrdersEntity;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.util.List;

public class UpLoadOrdersThread extends Thread {

    private static boolean isRun = false;

    Activity mActivity;

    private OrdersEntityDao ordersEntityDao;
    private OrdersDetailsEntityDao ordersDetailsEntityDao;

    private List<OrdersEntity> ordersEntityList;


    private int upProt = 0;

    public UpLoadOrdersThread(Activity activity) {
        this.mActivity = activity;
        ordersEntityDao = AppContext.getInstance().getDaoSession().getOrdersEntityDao();
        ordersDetailsEntityDao = AppContext.getInstance().getDaoSession().getOrdersDetailsEntityDao();
    }

    @Override
    public void run() {
        if (isRun)
            return;
        isRun = true;
        ordersEntityList = ordersEntityDao.queryBuilder().where(OrdersEntityDao.Properties.UpLoad.eq(false)).list();

        if (ordersEntityList.size() > 0) {
            upProt = 0;
            sendRs(ordersEntityList.get(upProt));
        } else {
            isRun = false;
        }

    }


    private void sendRs(OrdersEntity ordersEntity) {
        List<OrdersDetailsEntity> ordersDetailsEntity =
                ordersDetailsEntityDao.queryBuilder().where(OrdersDetailsEntityDao.Properties.Orders.eq(ordersEntity.getOrders())).list();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceId", SystemUtils.getSerialNumber());
        jsonObject.put("orders", ordersEntity);
        jsonObject.put("details", ordersDetailsEntity);
        try {
            RequestUtils.unOnLine(mActivity, jsonObject, new MyObserverRes<String>(mActivity, false) {
                @Override
                public void onSuccess(String result) {
                    try {
                        System.out.println("请求成功");
                        if (upProt < ordersEntityList.size() - 1) {
                            sendRs(ordersEntityList.get(++upProt));
                        } else {
                            isRun = false;
                        }
                        ordersEntity.setUpLoad(true);
                        ordersEntityDao.update(ordersEntity);
                    } catch (Exception e) {
                        e.printStackTrace();

                    }

                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    try {
                        System.out.println("请求失败");
                        if (upProt < ordersEntityList.size() - 1) {
                            sendRs(ordersEntityList.get(++upProt));
                        } else {
                            isRun = false;
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                }
            });

        } catch (Exception e) {
            isRun = false;
        }


    }
}
